﻿{extends file="../home.html"}

{block name="title"}{$head_title}{/block}
	
{block name="css"}
{/block}

{block name="content"}
<!--s主内容-->
<section class="content-wrapper">
  <div class="content-container container">
  	<!-- s面包屑导航 -->
  	{include file="../breadcrum_nav.html"}
  	<!-- e面包屑导航 -->
    <!-- s分类 -->
    <div class="col-left">
    {include file="../common_personal_nav.html"}
	</div>	
    <!-- e分类 -->
    <!-- s商品列表 -->
    <div class="col-main">
		{include file="../common_personal_header.html"}
		<!-- s主内容 -->
		<div class="user-main">
			<table class="table table-striped table-bordered table-condensed">
				<thead>
					<tr>
						<th>常购商品</th>
					</tr>
				</thead>
				{if !$item_list}
				<thead>
					<tr>
						<th style="text-align:center;font-weight:normal;">暂无常购商品</th>
					</tr>
				</thead>
				{/if}
				{if $item_list}
			    <thead>
		          <tr>
		            <th>序号</th>
		            <th>商品名称</th>
		            <th>购买数量</th>
		          </tr>
		        </thead>
		        <tbody>
		        {foreach from=$item_list name=loop item=items}
		          {foreach from=$items.item_info item=item}
		          <tr class="attr_select_content">
		            <td>{$smarty.foreach.loop.iteration-1}</td>
		            <td><a href="{$item_detail_link}{$item.item_id}">{$item.item_name}</a></td>
		            <td><input class="daily_itemNum" name="number" type="text" onkeyup="write_number(this)"></td>
		             <input type="hidden" name="item_id" class="item_id" value="{$item.item_id}">
			        <input type="hidden" name="item_name" class="item_name" value="{$item.item_name}">
			        <input type="hidden" name="mall_price" class="mall_price" value="{$item.vip_price}">
			        <input type="hidden" name="small_pic" class="small_pic" value="{$item.small_img}">
			        <input type="hidden" name="integral_num" class="integral_num" value="{$item.integral_num}">
		          </tr>
		          {/foreach}
		        {/foreach}
		        </tbody>
		        {/if}
		      </table>
     		  <a href="javascript:;" onClick="addCart(1);" class="form-button">加入购物车</a>

		</div>
		<!-- e主内容 -->

	</div>
	<div class="clearfix"></div>
    <!-- e商品列表 -->
  </div>
</section>
<!--e主内容-->
{/block}

{block name="js"}
<script>
{literal}
var first_order = 1;
$(function(){
	//小计计算
	$('.daily_itemNum').blur(function(){
		var _this = $(this);
		if(!_this.val().match(/^\d+$/) && _this.val() != ""){
			alert('请输入正确的数量');
			return;
		}
	});
});
function write_number(t)
{
      t.value = t.value.replace(/[^\d]/g,'');
}
//加入购物车
function addCart(type)
{
  var add_item_num = 0;
  var item_str = '';

  $('.attr_select_content').each(function(){
    var number = parseInt($(this).find('.daily_itemNum').val());
    if (number > 0)
    {
      item_str += $(this).find('.item_name').val() + ',';
      item_str += $(this).find('.mall_price').val() + ',';
      item_str += $(this).find('.small_pic').val() + ',';
      item_str += number + ',';
      item_str += $(this).find('.item_id').val() + ',';
      item_str += $(this).find('.integral_num').val() + ';';
      add_item_num += number;
    }
  });

  item_str = item_str.substr(0, item_str.length - 1);

  if (add_item_num == 0)
  {
    alert('抱歉，请至少选择一个商品！');
    return false;
  }

  $.post('/FrontCart/add_cart', {'first_order':first_order, 'item_str': item_str}, function(data){   
    if (data != 'failure')
    {
      if (type == 1)
      {
        alert('加入购物车成功');
        location.href = '/FrontCart/cart.html';
      }
      else
      {
        //进入订单购买页
        $('#shopping_cart_id_list').val(data.shopping_cart_id_list);
        $('#number_list').val(data.number_list);
        console.log(data);
        $('#buy_form').submit();

        //location.href = '/FrontCart/cart.html';
      }

    }
    else
    {
      alert('对不起，加入购物车失败');

    }
  }, 'json');
}
{/literal}
</script>
{/block}
